package com.gxa.jd.controller.front;


import com.gxa.entity.Result;
import com.gxa.exception.CustomerException;
import com.gxa.exception.MessageCode;
import com.gxa.jd.dto.EsSearchDto;
import com.gxa.jd.es.EsHotel;
import com.gxa.jd.service.HotelSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;


@RestController
@ResponseBody
@RequestMapping("/front/search")
@Api(description = "酒店搜索相关接口")
@Slf4j
public class HotelSearchController {

    @Autowired
    private HotelSearchService hotelSearchService;


    @PostMapping("/findHotel")
    @ApiOperation(value = "地理位置搜索酒店", notes = "地理位置搜索酒店", httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功! data代表返回的数据"),
    })
    public Page<EsHotel> findPage(@RequestBody  EsSearchDto esSearchDto){
        log.info("前端传递的数据为{}",esSearchDto);
        if(esSearchDto.getPage()<1){
            throw new  CustomerException(MessageCode.VALIDATE_PAGE);
        }
        Page<EsHotel> page = hotelSearchService.findPage(esSearchDto.getSearch(), esSearchDto.getLatitude(), esSearchDto.getLongitude(), esSearchDto.getDistance(), PageRequest.of(esSearchDto.getPage() - 1, esSearchDto.getPageSize()));
        return page;
    }


    @GetMapping("/findById")
    @ApiOperation(value = "从es获取酒店详情", notes = "从es获取酒店详情", httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功! data代表返回的数据"),
    })
    public Result<EsHotel> findById(Integer id) {
        log.info("前端查询酒店id{}",id);
        EsHotel esHotel = hotelSearchService.findHotelById(id);
        return new Result<>(esHotel);
    }




}
